package com.zt.dev.simulationModel.controller;

import com.zt.dev.common.ServerResponse;
import com.zt.dev.semiPhysicalEquipment.util.PageBean;
import com.zt.dev.simulationModel.service.Number_Simulation_Service;
import com.zt.dev.simulationModel.utils.ExcelUtil;
import com.zt.dev.simulationModel.vo.Number_Simulation;
import com.zt.dev.simulationModel.vo.Number_SimulationEnum;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 卜德凯
 * @date 2020/2/11 14:57
 */
@RestController
@RequestMapping("/simulation")
public class Number_Simulation_Controller {

    @Autowired
    private Number_Simulation_Service simulationService;

    /**
     * 分页查看装备模型列表
     * @return
     */
    @RequestMapping("/pagingFindModel")
    public ServerResponse findModelList(@RequestParam(value = "currentPage", defaultValue = "1", required = true) int currentPage, int pageSize){
        PageBean<Number_Simulation> pageBean = simulationService.pagingFindModel(currentPage, pageSize);
        if (!CollectionUtils.isEmpty(pageBean.getLists())) {
            return ServerResponse.createBySuccess(pageBean);
        }
        return ServerResponse.createByErrorMessage("数据为空");
    }

    /**
     * 根据id查看模型详情
     * @param id
     * @return
     */
    @RequestMapping("/findModelDetailsById")
    public ServerResponse findModelDetailsById(Integer id){
        if(id != 0 && id != null){
            List<Number_Simulation> simulationList = simulationService.findModelDetailsById(id);
            return ServerResponse.createBySuccess(simulationList);
        }
        return ServerResponse.createByErrorMessage("参数错误！");
    }

    /**
     * 删除模型
     * @param id
     * @return
     */
    @RequestMapping("/deleteModel")
    public ServerResponse deleteModel(Integer id){
        if(id != 0 && id != null){
            Integer result = simulationService.deleteModel(id);
            return ServerResponse.createBySuccessMessage("删除成功！");
        }
        return ServerResponse.createByErrorMessage("删除失败！");
    }

    /**
     * 根据id修改模型
     * @param numberSimulation
     * @return
     */
    @RequestMapping("/updateModelById")
    public ServerResponse updateModelById(Number_Simulation numberSimulation){
        if(numberSimulation != null){
            Integer result = simulationService.updateModelById(numberSimulation);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("修改成功！");
            }
            return ServerResponse.createByErrorMessage("修改失败！");
        }
        return ServerResponse.createByErrorMessage("参数有误！");
    }

    /**
     * 保存模型
     * @param numberSimulation
     * @return
     */
    @RequestMapping("/insertModel")
    public ServerResponse insertModel(Number_Simulation numberSimulation){
        if(numberSimulation != null){
            Integer result = simulationService.insertModel(numberSimulation);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("保存成功！");
            }
            return ServerResponse.createByErrorMessage("参数有误！");
        }
        return ServerResponse.createByErrorMessage("参数有误！");
    }

    /**
     * 根据id复制模型
     * @param id
     * @return
     */
    @RequestMapping("/copyModelById")
    public ServerResponse copyModelById(Integer id){
        if(id != 0 && id != null){
            //根据id获取要复制模型的数据
            Number_Simulation numberSimulation = simulationService.findModelById(id);
            if(numberSimulation != null){
                //把获取到的模型数据擦插入表中
                Integer result = simulationService.copyModelById(numberSimulation);
                if(result > 0){
                    return ServerResponse.createBySuccessMessage("复制成功！");
                }
                return ServerResponse.createByErrorMessage("复制失败！");
            }
            return ServerResponse.createByErrorMessage("获取的参数有误！");
        }
        return ServerResponse.createByErrorMessage("传入的参数有误！");
    }

    /**
     * 导入模型
     * @param request
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/importModel")
    @ResponseBody
    public ServerResponse importModel(HttpServletRequest request, MultipartFile file)throws Exception{
        ExcelUtil excelUtil = new ExcelUtil(Number_SimulationEnum.objMap);
        //try {
            List<Object> simulationList = excelUtil.getDataFromExcel(file, Number_Simulation.class);
            for(int i =0;i<simulationList.size();i++){
                Number_Simulation numberSimulation = (Number_Simulation) simulationList.get(i);
                if(numberSimulation.getCrateTime() != null && !"".equals(numberSimulation.getCrateTime())){
                    simulationService.insertModel(numberSimulation);
                    return ServerResponse.createBySuccessMessage("导入成功！");
                }
            }
            System.gc();
        //}catch (Exception e ){
            return ServerResponse.createByErrorMessage("导入失败！");
        //}
        //return ServerResponse.createByErrorMessage("导入数据时出现错误！");
    }

    /**
     * 导出模型
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/exportModel")
    public void exportModel(HttpServletResponse response)throws Exception{
        String paperTitle = "数字仿真模型";
        String fileName = paperTitle + ".xls";
        List<Number_Simulation> simulationList = simulationService.findModelList();
        String[] title1 = new String[]{"序号,1,2"};
        String[] title2 = new String[]{"仿真模型名称", "装备类型", "装备编号", "模型开发单位", "主要性能", "射程","速度","高度","接口参数","创建时间","修改时间"};
        String[] column = new String[]{"modelName", "equipmentType", "equipmentNum", "modelCompany", "performance", "range","speed","altitude","interfaceParam","crateTime","updateTime"};
        HSSFWorkbook wb = new ExcelUtil().getHSSFWorkbook(paperTitle,title1,title2,simulationList,column);
        fileName = new String(fileName.getBytes(),"ISO8859-1");
        response.setContentType("application/octet-stream;charset=ISO8859-1");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        response.addHeader("Pargam","no-cache");
        response.addHeader("Cache-Control","no-cache");
        OutputStream os = response.getOutputStream();
        wb.write(os);
        os.flush();
        os.close();
    }

}
